import instance  from "../utils/request.js";

//其中的传的data都是一个封装好的对象
export async function userRegister(User){
    try {
        const res = await instance({
            url: '/user/register',
            method: 'post',
            data: User
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}


export async function userLogin(User){
    try {
        const res = await instance({
            url: '/user/login',
            method: 'post',
            data: User
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function userForgetPassword(User){
    try {
        const res = await instance({
            url: '/user/forgetPassword',
            method: 'post',
            data: User
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function getUserInfo(userId){
    try {
        const res = await instance({
            url: `/user/${userId}`,
            method: 'get',
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function updateUserInfo(User){
    try {
        const res = await instance({
            url: `/user/update`,
            method: 'put',
            data: User
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function uploadUserAvatar(userId, base64Image){
    try {
        const res = await instance({
            url: 'user/avatar/upload/base64',
            method: 'post',
            params: {
                userId: userId,
                base64Image: base64Image
            }
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function downloadUserAvatar(avatarUrl){
    try {
        const res = await instance({
            url: `/user/avatar/${avatarUrl}`,
            method: 'get',
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function uploadUserAvatarFile(userId, file){
    try {
        const res = await instance({
            url: '/user/avatar/upload/file',
            method: 'post',
            data: file,
            params: {
                userId: userId
            }
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function downloadUserAvatarByUserId(userId){
    try {
        const res = await instance({
            url: `/user/avatar/${userId}`,
            method: 'get',
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}

export async function sendCode(code){
    try {
        const res = await instance({
            url: '/user/sendcode',
            method: 'post',
            data: code
        });
        return res;
    } catch (err) {
        console.log(err);
    }
}